#include "../include/head.h"
//copy sb server_dir.c

int client_puts_comfirm(int target,int args_len,char *args,int netfd,char *now_path_buf)
{
    shareRes_t *upload_share = (shareRes_t *)malloc(sizeof(shareRes_t));

    char argbuf[2048]={0};
    memcpy(argbuf,args,args_len);
    char *addr1=strtok(argbuf," ");//客户端文件的地址
    char *addr2=strtok(NULL," ");//服务端要存储的文件的地址
    char *md5=strtok(NULL," ");
    char *filesizebuf=strtok(NULL," ");
    char *father_netfd = strtok(NULL, " ");
    char *user_id = strtok(NULL, " ");
    char *now_path_id = strtok(NULL, " ");

    off_t netfilesize = 0;

    struct stat file_stat;
    if (stat(addr1, &file_stat) == -1) {
        printf("server_puts_query stat error\n");
        return -1;
    }
    off_t filesize = file_stat.st_size;

    sscanf(filesizebuf,"%ld",&netfilesize);

    if(netfilesize == filesize)
    {
        printf("极速快传已完成！\n");
        return -1;
    }

    if(netfilesize != 0)
    {
        printf("指定路径下已有同名文件，是否继续？%ld\n",netfilesize);
        printf("请输入(0:取消 1:续写 2:覆写 ):");
        scanf("%d", &upload_share->client_choose);
        fflush(stdin);
    }
    else{
        upload_share->client_choose = 3;
    }

    upload_share->type = C_OP_PUTS_BEGIN;
    upload_share->args_len = args_len;
    upload_share->file_size = filesize;
    memcpy(upload_share->agrs, args, args_len);

    pthread_t tid1;
    pthread_create(&tid1, NULL, upload_enter, upload_share);
       
    // 分离线程（线程结束后自动回收资源）
    pthread_detach(tid1);

    printf("任务已移交给线程，您可执行其他指令\n");

    return 1;
}

int client_gets_comfirm(int target,int args_len,char *args,int netfd,char *now_path_buf)
{
    shareRes_t *download_share = (shareRes_t *)malloc(sizeof(shareRes_t));

    char argbuf[512]={0};
    memcpy(argbuf,args,args_len);
    char *addr1=strtok(argbuf," ");//客户端文件的地址
    char *addr2=strtok(NULL," ");//服务端要存储的文件的地址
    char *netfilesizebuf=strtok(NULL," ");
    char* md5 = strtok(NULL," ");

    char *father_netfd = strtok(NULL, " ");
    char *user_id = strtok(NULL, " ");
    char *now_path_id = strtok(NULL, " ");
    off_t netfilesize = 0;
    sscanf(netfilesizebuf,"%ld",&netfilesize);

    char *filename;
    char *fileFatherPath;
    split_path(addr1,&fileFatherPath,&filename);

    char localFilePath[512] = {0}; 
    if(addr2[strlen(addr2)-1]=='/')
    {
        sprintf(localFilePath,"%s%s",addr2,filename);
    }
    else
    {
        sprintf(localFilePath,"%s/%s",addr2,filename);
    }
    // printf("%s %s %s %s\n", addr1, addr2, filename, localFilePath);
    // pause();


    off_t filesize = 0;
    if(access(localFilePath,F_OK)!=-1)
    {
        struct stat file_stat;
        if (stat(localFilePath, &file_stat) == -1) {
            //printf("server_gets_query open filePath = %s\n", filePath);
            //printf("server_puts_query 5 stat error\n");
            free(filename);
            free(fileFatherPath);
            return -1;
        }

        filesize = file_stat.st_size;
        printf("本地路径下存在同名文件: %s,是否继续?\n",localFilePath);
        if(filesize < netfilesize)
        {
            printf("请输入(0:取消 1:覆写 2:续写):");
            fflush(stdout);
            int userop;
            scanf("%d", &download_share->client_choose);
            if(&download_share->client_choose == 0)
            {
                printf("用户取消下载\n");
                free(filename);
                free(fileFatherPath);
                return -1;
            }
            if(download_share->client_choose != 0 &&download_share->client_choose != 1 &&download_share->client_choose != 2)
            {
                printf("输入错误！\n");
                free(filename);
                free(fileFatherPath);
                return -1;

            }
            fflush(stdin);
        }
        else
        {
            printf("请输入(0:取消 1:覆写):");
            fflush(stdout);
            scanf("%d",&download_share->client_choose);
            if(download_share->client_choose == 0)
            {
                printf("用户取消下载\n");
                free(filename);
                free(fileFatherPath);
                return -1;
            }
            if(download_share->client_choose != 0 &&download_share->client_choose != 1)
            {
                printf("输入错误！\n");
                free(filename);
                free(fileFatherPath);
                return -1;
            }
        }
    }
    else
    {
        download_share->client_choose = 9;
    }

    download_share->type = C_OP_GETS_BEGIN;
    download_share->args_len = args_len;
    memcpy(download_share->agrs, args, args_len);
    download_share->file_size = filesize;

    pthread_t tid2;
    printf("创建子线程！\n");
    pthread_create(&tid2, NULL, download_enter, download_share);

    // 分离线程（线程结束后自动回收资源）
    pthread_detach(tid2);

    printf("任务已移交给线程，您可执行其他指令\n");

    free(filename);
    free(fileFatherPath);
    // printf("gets end\n");
    return 0;
}
